<template>
    <div class="resetPassword">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row>
        <el-col :span="12" :offset="2">
          <el-form ref="resetPassword" class="resetPass-container"  :model="user" :rules="rules"  :span="12" label-width="100px" label-position="left" >
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input placeholder="请输入旧密码" v-model="user.oldPassword"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input placeholder="请输入新密码" v-model="user.newPassword"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input placeholder="请确定新密码" v-model="user.confirmPassword"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="btn_submit" @click="submit('resetPassword')">确定</el-button>
              <el-button class="btn_reset" @click="reset('resetPassword')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>

</template>

<script>
    import ElRow from 'element-ui/packages/row/src/row';

    export default {
      components: {ElRow},
      data() {
        var validatePass1 = (rule, value, callBack) => {
          if (value === '') {
            callBack(new Error('请输入旧密码'));
          }
          let reg = /^[A-Za-z0-9]{6,16}$/;
          if (!reg.test(value)) {
            callBack(new Error('旧密码格式不正确'));
          }
          callBack();
        };
        var validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入新密码'));
          }
          let reg = /^[A-Za-z0-9]{6,16}$/;
          if (!reg.test(value)) {
            callback(new Error('请输入6-12位有效字符作为新密码'));
          }
          if (this.user.confirmPassword !== '') {
            this.$refs.resetPassword.validateField('confirmPassword');
          }
          callback();
        };
        var validatePass3 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入新密码'));
          } else if (value !== this.user.newPassword) {
            callback(new Error('两次输入的密码不一致'));
          } else {
            callback();
          }
        };
        return {
          user: {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
          },
          rules: {
            oldPassword: [
              {
                validator: validatePass1,
                trigger: 'blur'
              }
            ],
            newPassword: [
              {
                validator: validatePass2,
                trigger: 'blur'
              }
            ],
            confirmPassword: [
              {
                validator: validatePass3,
                trigger: 'blur'
              }
            ]
          }
        };
      },
      methods: {
        reset(formName) {
          this.$refs[formName].resetFields();
        },
        submit(formName) {
          this.$refs[formName].validate((valid) => {
            console.log(valid);
            if (valid) {
              console.log(1);
              this.resetPassword();
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        resetPassword() {
          this.$http.post('/api/host/user/resetPassword.do', this.$qs.stringify({
            'oldPassword': this.user.oldPassword,
            'newPassword': this.user.newPassword
          }))
            .then((response) => {
              var data = response.data;
              console.log(data);
              if (data.status === 0) {
                this.$message({
                  message: `${data.msg},将在1秒后跳转登录页面`,
                  tpye: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.$router.push({path: '/login'});
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch((error) => {
              this.$message.error(error.msg);
            });
        }
      }
    };
</script>

<style scoped>
  .resetPass-container{
    margin-top: 50px;
  }
  .btn_content{
    margin-top: -50px;
  }
</style>
